home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0022_Orbit Simulator.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  6.8 KB  |  257 lines

  1.  (*-------------------------------------------------------------------------
  2.  little orbit simulator I just wrote (I guess thats what you could call it)
  3.  I was going to add more use with the mouse.. and maybe make it into a
  4.  starcontrol 2 type game, but ..I should really do that in c :) I hate using
  5.  C tho! :) Borland, please come out with BP 8 !
  6.  
  7.  code is.. freeware
  8.  
  9.  mail me if you have any comments/questions/suggestions
  10.  I dont know if the math is 100% correct, but I'd like it to be as correct
  11.  as possible.. if you add anything to this, mail it to me
  12.  
  13.  ryan@emiko.igcom.net
  14.  (or if that fails, ryan@ripco.com)
  15.  --------------------------------------------------------------------------*)
  16.  
  17.  {$G+,N+,E+}
  18.  Uses mymouse, {stuff,} Crt;  { mymouse is in MOUSE.SWG !! }
  19.  
  20.  Type
  21.    Thing=record
  22.      mass: real;
  23.      x,y, Oldx,Oldy: real; {position}
  24.      vx, vy : real; {x and y velocity}
  25.    end;
  26.  
  27.  Var
  28.    ch:char;
  29.    st,s:string;
  30.    ok:boolean;
  31.    loops:longint;
  32.    i:integer;
  33.    ob,test:thing;
  34.    oba:array[1..20] of thing;
  35.    {--config stuff--}
  36.    B,trail:byte;
  37.    thedelay:word;
  38.    do1,mouseshown:boolean;
  39.    scale,maxspeed:integer;
  40.    lastmouseuse:word;
  41.    TotalThings,constant,gr:integer;
  42.  
  43.    {can you tell I wrote this in 2 hours?}
  44.  
  45.    procedure config;
  46.    var t:text;
  47.        i:integer;
  48.    begin
  49.      gr:= -1;            {if -1, gravity, if 1, repluse }
  50.      constant:=1;        {constant of the universe "or something", 1}
  51.      do1:=false;         {false=obj 1 is center of universe}
  52.      trail:=0;           {0=none 1=grey 2=color}
  53.      scale:=10;          {drawing scale (pixles=mass/scale) not at all accurate, just for show}
  54.      maxspeed:=5;        {speed of light? :) fastest anything can go. 5. }
  55.  
  56.      {you might not have a pentium 90.. :) if so lower this}
  57.      {*******}
  58.      thedelay:=80;       {milisecond delay in main loop.. 80 on my pentium90}
  59.      {********}
  60.  
  61.      mouseshown:=true;   {is mouse cursor shown on screen right now}
  62.      lastmouseuse:=0000; {not used now}
  63.  
  64.  {assign(t,'data.dat');
  65.  reset(t);}
  66.  i:=0;
  67.  {totalthings:=0;
  68.  repeat
  69.    inc(i);
  70.    inc(totalthings);
  71.    with oba[i] do begin
  72.      readln(t,mass);
  73.      readln(t,x);
  74.      readln(t,y);
  75.      readln(t,vx);
  76.      readln(t,vy);
  77.      oldx:=0;
  78.      oldy:=0;
  79.    end;
  80.  until eof(t);
  81.  close(t);
  82.  }
  83.      totalthings:=3;
  84.      with oba[1] do begin
  85.        oldx:=10; oldy:=10;
  86.        mass:=70;
  87.        x:=150;
  88.        y:=100;
  89.        vx:=1;
  90.        vy:=1;
  91.      end;
  92.      with oba[2] do begin
  93.        mass:=4;
  94.        x:=21;
  95.        y:=84;
  96.        vx:=2;
  97.        vy:=0;
  98.      end;
  99.      with oba[3] do begin
  100.        mass:=18;
  101.        x:=122;
  102.        y:=122;
  103.        vx:=0;
  104.        vy:=1;
  105.      end;
  106.    end;
  107.  
  108.  { ---------------------------------------------------------------------- }
  109.    procedure SETMODE(mode : byte); assembler;
  110.    asm
  111.      MOV AH,0
  112.      MOV AL,MODE
  113.      INT $10
  114.    end;
  115.    (*
  116.    begin
  117.        regs.ah := 0;
  118.        regs.al := mode;
  119.        intr($10, regs)
  120.    end;
  121.    *)
  122.  
  123.    function i2s(i: Longint): string; {integer to string}
  124.    var
  125.      s: string[11];
  126.    begin
  127.      Str(i, s);
  128.      I2s := s;
  129.    end;
  130.  
  131.    Procedure plot286(x,y:integer; c:byte);
  132.    {very fast putpixel that uses 286 instructions}
  133.    Inline(
  134.       $58/$B9/$00/$A0/$8E/$C1/
  135.       $5B/$88/$DD/$5F/$01/$CF/
  136.       $C1/$E9/$02/$01/$CF/$AA);
  137.  
  138.  { ---------------------------------------------------------------------- }
  139.  
  140.    function update(var it:thing):string;
  141.    var
  142.     t,ax,ay,x2,y2,k:real;
  143.     a,dist,tempreal:real;
  144.     sx,sy,tempi:integer;
  145.     ret:string;
  146.    begin
  147.      with it do begin
  148.        Oldx:=x;
  149.        Oldy:=y;
  150.        ax:=0;
  151.        ay:=0;
  152.  
  153.        x:=x+vx;
  154.        y:=y+vy;
  155.        for tempi:= 1 to totalthings do begin
  156.          x2:=oba[tempi].x;
  157.          y2:=oba[tempi].y;
  158.          if (x<>x2) and (y<>y2) then begin
  159.            tempreal:=((sqr(x-x2)+sqr(y-y2)));
  160.            if tempreal<1 then tempreal:=1;{they touched..}
  161.            {do this later:                 so add everything and kill one }
  162.            k:=(oba[tempi].mass){ * mass)} * constant ;
  163.            dist:=gr* (k / tempreal);
  164.            a:=arctan((y-y2) / (x-x2));
  165.            if x<x2 then a:=PI-a;
  166.          end;
  167.          ax:=( dist*cos(a) )+ax;
  168.          if x>=x2 then ay:=( dist*sin(a) )+ay else ay:=( dist * -sin(a) )+ay;
  169.        end;
  170.        vx:=vx+ax;
  171.        vy:=vy+ay;
  172.        {---}
  173.        if vy>maxspeed then vy:=maxspeed;
  174.        if vx>maxspeed then vx:=maxspeed;
  175.        if vy<-maxspeed then vy:=-maxspeed;
  176.        if vx<-maxspeed then vx:=-maxspeed;
  177.        {gotoxy(1,24); write(a);}
  178.        ret:=' ax: '+i2s(trunc(ax))+' ay: '+i2s(trunc(ay))+' ';
  179.        {--screen edge stuff, more realistic without these 4 lines:----}
  180.        if x<1 then x:=299;
  181.        if x>299 then x:=1;
  182.        if y<1 then y:=199;
  183.        if y>199 then y:=1;
  184.        update:=ret;
  185.      end;
  186.    end;
  187.  
  188.    procedure draw(it:thing; color:integer);
  189.    var c,i,i2:integer;
  190.    begin
  191.      {this won't let it draw off screen}
  192.      for i:=0 to trunc(it.mass / scale) do with it do begin
  193.        for i2:=0 to trunc(mass / scale) do if ((oldy+i<200) and (oldx+i2<300)) then begin
  194.          if trail=0 then c:=0 else
  195.          if trail=1 then c:=8 else
  196.          c:=color;
  197.          plot286(trunc(Oldx+i2),trunc(Oldy+i),c);
  198.        end;
  199.      end;
  200.  
  201.      for i:=0 to trunc(it.mass / scale) do with it do
  202.        for i2:=0 to trunc(mass / scale) do if ((y+i<200) and (x+i2<300)) then plot286(trunc(x+i2),trunc(y+i),color);
  203.    end;
  204.  { ---------------------------------------------------------------------- }
  205.  
  206.  begin
  207.    config;
  208.  
  209.    loops:=0;
  210.    initmouse(b,ok);
  211.    if ok=false then begin
  212.       writeln('no mouse found!');
  213.       halt;
  214.    end else writeln('mouse found, ',b,' buttons.');
  215.    setmode($13);
  216.    directvideo:=false;
  217.    showmouse;
  218.    repeat
  219.  
  220.      if mouseleftpressed then begin
  221.        mouseshown:=not mouseshown;
  222.        if mouseshown then showmouse else begin
  223.          {gotoxy(1,1); for i:=1 to 38 do write(' ');}
  224.          fillchar(ptr($A000,0)^,64000,0);
  225.          hidemouse;
  226.        end;
  227.      end else
  228.      if mouserightDOWN{pressed} then begin
  229.        if totalthings<20 then begin
  230.          inc(totalthings);
  231.          oba[totalthings].mass:=RANDOM(5)+1;
  232.          oba[totalthings].x:=mousex div 2;
  233.          oba[totalthings].y:=mousey;
  234.          oba[totalthings].vx:=1;
  235.          oba[totalthings].vy:=1;
  236.        end;
  237.        {BEEP;}
  238.      end;
  239.      if mouseshown then begin
  240.        getmousexy;
  241.        gotoxy(1,1); write('mouseX:',mousex,' mouseY:',mousey,'    ');
  242.      end;
  243.  
  244.      inc(loops);
  245.      s:='Loop:'+i2s(loops);
  246.      for i:=1 to TotalThings do
  247.        if (i<>1) or ((i=1) and (do1=true)) then st:=update(oba[i]);
  248.      s:=s+st;
  249.      for i:=1 to TotalThings do draw(oba[i],i);
  250.      {gotoxy(1,25); write(s);}
  251.      delay(thedelay);
  252.    until keypressed;
  253.    ch:=readkey;
  254.    textmode(co80);
  255.  end.
  256.  
  257.